///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.utils {
	/**
	 * Scales an object within an area while maintaining the original aspect ratio.  
	 */
	public class AspectScale {
		private var _startHeight:Number;
		private var _startWidth:Number;
		private var _newHeight:Number;
		private var _newWidth:Number;
		
		public function AspectScale(width:Number,height:Number) {
			_startHeight = height;
			_startWidth = width;
		}
		
		/**
		 * Returns the height and width values to scale a display object to fill the available space.
		 */
		public function getScaledDimensions(availWidth:Number,availHeight:Number):Object {
			if (_startHeight * availWidth / _startWidth >= availHeight) {
				_newHeight = _startHeight * availWidth / _startWidth;
				_newWidth = availWidth;
			} else if (_startWidth * availHeight / _startHeight >= availWidth) {
				_newHeight = availHeight;
				_newWidth = _startWidth * availHeight / _startHeight;
			}
			
			return {width:_newWidth,height:_newHeight};
		}
		
		/**
		 * Returns the height and width values to scale a display object within the available space.
		 */
		public function getContainedDimensions(availWidth:Number,availHeight:Number):Object {
			if (_startHeight * availWidth / _startWidth <= availHeight) {
				_newHeight = _startHeight * availWidth / _startWidth;
				_newWidth = availWidth;
			} else if (_startWidth * availHeight / _startHeight <= availWidth) {
				_newHeight = availHeight;
				_newWidth = _startWidth * availHeight / _startHeight;
			}
			
			return {width:_newWidth,height:_newHeight};
		}
	}
}